<html>
<head><meta charset="utf-8"><title>Joining one thread out of many? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Joining.20one.20thread.20out.20of.20many.3F.html">Joining one thread out of many?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="186829807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Joining%20one%20thread%20out%20of%20many%3F/near/186829807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Joining.20one.20thread.20out.20of.20many.3F.html#186829807">(Jan 28 2020 at 21:15)</a>:</h4>
<p>This came up in another thread (sic) -- I don't see any way to wait for one thread out of N to finish (except maybe spawning N more threads that call <code>join</code> on the original ones and send the results over a channel type that supports <code>select</code>).</p>



<a name="186838424"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Joining%20one%20thread%20out%20of%20many%3F/near/186838424" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Joining.20one.20thread.20out.20of.20many.3F.html#186838424">(Jan 28 2020 at 22:49)</a>:</h4>
<p>This is not possible on Linux at least. On Windows it might be possible with WaitForMultipleObjects, but even then you're limited to 64 objects. Using <code>select</code> and channels is the only portable way.</p>



<a name="186838575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Joining%20one%20thread%20out%20of%20many%3F/near/186838575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Joining.20one.20thread.20out.20of.20many.3F.html#186838575">(Jan 28 2020 at 22:51)</a>:</h4>
<p>I see, thanks</p>



<a name="186959398"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Joining%20one%20thread%20out%20of%20many%3F/near/186959398" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Joining.20one.20thread.20out.20of.20many.3F.html#186959398">(Jan 30 2020 at 06:58)</a>:</h4>
<p>You can do it in application code: Add a guard in each thread, which signals a shared object (e.g. a condition variable, channel, manualresetevent, etc). Then wait on that object.</p>
<p>If you need to be sure that the thread is really joined and not only close to being joined, then you need to pass each thread some unique ID that you can send back via this channel. Then the waiter can identify the associated <code>JoinHandle</code> and join the thread.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>